<template>
	<view>
		<view class="">
			<u-navbar back-text="" title="申请记录" title-color="#000" :custom-back="()=>{
				vk.navigateBack()}"></u-navbar>
			<view v-if="empty"
				style="position: absolute;top: 50%;left: 50%;transform: translateX(-50%) translateY(-50%)" class="">
				<u-empty text="无申请记录,请先进行申请" mode="list"></u-empty>
			</view>
			<!-- <u-tabs :list="tabList" :is-scroll="false" :current="current" @change="tabChange"></u-tabs> -->

			<u-card padding="13" v-for="(item,index) in cardList" :key="item.stu_id" :title="title"
				:sub-title="item.type" border-radius="30">
				<view class="" slot="body">
					<u-row gutter="0">
						<u-col offset="1" span="3">
							<text style="font-weight: 700;">姓名</text>
						</u-col>
						<u-col span="4">
							{{item.name}}
						</u-col>

					</u-row>
					<u-row gutter="0">
						<u-col offset="1" span="3">
							<text style="font-weight: 700;">学号</text>
						</u-col>

						<u-col span="5">
							{{item.stuId}}
						</u-col>

					</u-row>

					<u-row gutter="0">
						<u-col offset="1" span="3">
							<text style="font-weight: 700;">申请状态</text>
						</u-col>

						<u-col span="5">
							<u-tag v-if="item.state=='0'" :type="applyState" text="审核中" mode="plain" />
							<view v-else class="">
								<u-tag v-if="item.state == '1'" type="success" text="审核通过" mode="plain" />
								<u-tag v-else-if="item.state == '2'" type="error" text="驳回申请" mode="plain" />
							</view>
						</u-col>

					</u-row>

					<u-row gutter="0" v-if="item.state=='2'">

						<u-col offset="1" span="3">
							<text style="font-weight: 700;">备注</text>
						</u-col>

						<u-col span="5">
							<view class="">
								<text>{{item.remark}}</text>
							</view>
						</u-col>

					</u-row>
					<u-row gutter="0" v-if="item.state=='2'">
					
						<u-col offset="1" span="3">
							<text style="font-weight: 700;">二审备注</text>
						</u-col>
					
						<u-col span="5">
							<view class="">
								<text>{{item.anotherRemark}}</text>
							</view>
						</u-col>
					
					</u-row>
					<u-row gutter="0">
						<u-col offset="1" span="3">
							<text style="font-weight: 700;">申请时间</text>
						</u-col>

						<u-col span="5">
							{{item.createTime?vk.pubfn.timeFormat(new Date(item.createTime),"yyyy-MM-dd hh:mm:ss"):vk.pubfn.timeFormat(new Date(item.updateTime),"yyyy-MM-dd hh:mm:ss")}}
						</u-col>

					</u-row>

					<u-row gutter="0">
						<u-col offset="1" span="3">

							<u-button @click="revoke(item.docId,item.type)" type="error" style="width: 100rpx;"
								size="medium">撤回申请</u-button>

						</u-col>

						<u-col offset="1" span="3">

							<u-button @click="downloadWord(item.type)" type="success" style="width: 100rpx;"
								size="medium" plain>下载附件</u-button>
						</u-col>

						<u-col offset="1" span="3" v-if="item.type == '国家助学金'">

							<u-button @click="downloadPdf(item.type)" type="warning" style="width: 100rpx;"
								size="medium" plain>预览</u-button>
						</u-col>


					</u-row>
				</view>

			</u-card>

		</view>
		<u-loadmore status="nomore" :load-text="loadText" />
	</view>
</template>

<script>
	import {
		getApplyList,
		revoke
	} from "@/api/applyList.js"
	import {
		exportWord,
		exportPdf
	} from "@/api/guozhu.js"

	var that; // 当前页面对象
	var vk; // vk依赖
	export default {
		data() {
			return {
				current: 0,
				tabList: [{
					name: '春节补助'
				}],
				show: false,
				cardList: [
					// 	{
					// 	name:'梁万丰',
					// 	stuId:'1906440202',
					// 	state:'0',
					// 	createTime: '2022-07-22',
					// 	type:'困难生申请(校内)'
					// }
				],
				applyState: 'primary',
				title: '我的申请',
				subTitle: '2021-12',
				loadText: {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '下拉刷新'
				},
			}

		},
		computed: {
			empty() {
				if (this.cardList.length != 0) {
					return false
				}
				return true

			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			setTimeout(async () => {
				await this.getData()
				uni.stopPullDownRefresh()
				vk.toast('刷新成功', 'none');
			})
		},
		methods: {
			downloadPdf(type) {

				let params = {
					isPreview: 'true',
					Authorization: "Bearer " + uni.getStorageSync("stuToken"),

				}

				// 移除旧的节点
				const oldNode = document.querySelector("#form-pdf")
				if (oldNode) {
					document.body.removeChild(document.querySelector("#form-pdf"))
				}
				// 生成新节点，进行下载
				const form = document.createElement("form")
				form.style.display = "none"
				form.id = "#form-pdf"
				form.action = this.baseUrl + "/studentService/guozhu/exportPdf"
				form.method = 'get'
				form.target = "_blank"
				document.body.appendChild(form)

				// 动态创建 input 并给 value 赋值
				for (const key in params) {
					const input = document.createElement('input')
					input.type = 'hidden'
					input.name = key
					input.value = params[key]
					form.appendChild(input)
				}
				form.submit()
				form.remove()

				// exportPdf().then((data)=>{
				// 	let [ err, res ] = data
				// 	console.log(res)
				// 	if(res.data.code == 200){
				// 		// 移除旧的节点
				// 		  const oldNode = document.querySelector("#g-exportOrder-pdf-iframe")
				// 		  if (oldNode) {
				// 		    document.body.removeChild(document.querySelector("#g-exportOrder-pdf-iframe"))
				// 		  }
				// 		  // 生成新节点，进行下载
				// 		  const iframe = document.createElement("iframe")
				// 		  iframe.style.display = "none"
				// 		  iframe.id = "g-exportOrder-pdf-iframe"
				// 		  iframe.src = this.baseUrl + '/common/download?fileName='+res.data.msg+"&delete=false"
				// 		  document.body.appendChild(iframe)

				// 	}else {
				// 		vk.alert('失败')
				// 	}

				// })


			},


			downloadWord(type) {
				let params = {
					isPreview: 'false',
					Authorization: "Bearer " + uni.getStorageSync("stuToken"),

				}
				switch (type) {
					case '国家助学金': {
						//TODO linux系统缺失字体文件，转pdf会有文字丢失，需要从windows复制字体文件到linux
						
						
						// 移除旧的节点
						const oldNode = document.querySelector("#form-pdf")
						if (oldNode) {
							document.body.removeChild(document.querySelector("#form-pdf"))
						}
						// 生成新节点，进行下载
						const form = document.createElement("form")
						form.style.display = "none"
						form.id = "#form-pdf"
						form.action = this.baseUrl + "/studentService/guozhu/exportPdf"
						form.method = 'get'
						form.target = "_blank"
						document.body.appendChild(form)

						// 动态创建 input 并给 value 赋值
						for (const key in params) {
							const input = document.createElement('input')
							input.type = 'hidden'
							input.name = key
							input.value = params[key]
							form.appendChild(input)
						}
						form.submit()
						form.remove()
					}


					// exportWord().then((data)=>{
					// 	let [ err, res ] = data
					// 	console.log(res)
					// 	if(res.data.code == 200){
					// 		// 移除旧的节点
					// 		  const oldNode = document.querySelector("#g-exportOrder-iframe")
					// 		  if (oldNode) {
					// 		    document.body.removeChild(document.querySelector("#g-exportOrder-iframe"))
					// 		  }
					// 		  // 生成新节点，进行下载
					// 		  const iframe = document.createElement("iframe")
					// 		  iframe.style.display = "none"
					// 		  iframe.id = "g-exportOrder-iframe"
					// 		  iframe.src = this.baseUrl + '/common/download?fileName='+res.data.msg+"&delete=false"
					// 		  document.body.appendChild(iframe)
					// 	}else {
					// 		vk.alert('失败')
					// 	}

					// })
					break
					case '困难生申请(校内)': {
						vk.toast('开发中', 'none');
						// 移除旧的节点
						const oldNode = document.querySelector("#form-word1")
						if (oldNode) {
							document.body.removeChild(document.querySelector("#form-word1"))
						}
						// 生成新节点，进行下载
						const form = document.createElement("form")
						form.style.display = "none"
						form.id = "#form-word1"
						form.action = this.baseUrl + "/studentService/exportWord"
						form.method = 'post'
						form.target = "_blank"
						document.body.appendChild(form)

						// 动态创建 input 并给 value 赋值
						for (const key in params) {
							const input = document.createElement('input')
							input.type = 'hidden'
							input.name = key
							input.value = params[key]
							form.appendChild(input)
						}
						form.submit()
						form.remove()
						break
					}
				}


			},
			// 撤回申请
			revoke(docId, type) {
				switch (type) {
					case '国家助学金':
						vk.alert('暂不支持撤回')
						break
					case '困难生申请(校内)':
						vk.confirm("是否确认撤回申请?此操作无法撤回!", (res) => {
							if (res.confirm) {
								// 点击确定按钮后的回调
								revoke({
									docId
								}).then((data) => {
									let [err, res] = data
									if (res.data.code == 200) {
										vk.toast('撤回成功', 'success');
										vk.setVuex('$app.isSubmit', true)
										for (let var1 in this.cardList) {
											if (this.cardList[var1].docId = docId) {
												this.cardList.splice(var1, 1)
											}
										}
									} else {
										vk.alert(res?.data.msg || err);
									}
								})
							}
						});
						break
				}

			},
			getData(args) {
				getApplyList({
					pageNum: 1,
					pageSize: 10,
				}).then((data) => {
					let [err, res] = data

					if (res.data.code == 200) {
						this.cardList = res.data.rows
					} else {
						vk.alert(res.data?.msg || err);
					}
				})
			},
			tabChange(e) {
				this.current = e
				this.cardList = []
				this.$nextTick(() => {
					uni.showLoading({
						title: '请求中'
					})
				})
			},
		},
		onLoad(options) {
			// 初始化vk框架
			that = this;
			vk = that.vk;
			that.options = options;

			this.getData()

		},
		onReady() {

		}
	}
</script>

<style scoped>
	.wrap {
		padding: 24rpx;
	}

	.u-row {
		margin: 20rpx 0;
	}
</style>